<template>
	<div class="system-dept-container">
		<!-- 查询条件-->
		<el-card shadow="hover">
			<el-form inline v-model="params" size="small">
				<el-form-item>
					<el-input v-model="params.tenantCode" placeholder="搜索租户编码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="params.companyName" placeholder="搜索公司名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="initTableData">
						<el-icon>
							<elementSearch />
						</el-icon>
						查询
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<!--分割线-->
		<el-divider></el-divider>
		<!--数据展示和分页-->
		<el-card shadow="never">
			<template #header>
				<div class="card-header">
					<span>租户列表</span>
					<!--新增等操作区域-->
					<el-button size="small" type="success" class="ml10" @click="openAddWindow">
						<el-icon>
							<elementDocumentAdd />
						</el-icon>
						新增租户
					</el-button>
				</div>
			</template>
			<!--表格展示区-->
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column prop="tenantId" label="租户ID" show-overflow-tooltip width="150" align="center" />
				<el-table-column prop="tenantCode" label="租户编码" show-overflow-tooltip width="150" align="center" />
				<el-table-column prop="companyName" label="公司名称" show-overflow-tooltip align="center" />
				<el-table-column prop="companyLogo" label="公司logo" show-overflow-tooltip width="130" align="center" />
				<el-table-column label="状态" show-overflow-tooltip width="70" align="center">
					<template #default="scope">
						<el-tag type="success" v-if="Date.parse(scope.row.overdueTime) > new Date()">正常</el-tag>
						<el-tag type="warning" v-else>过期</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="overdueTime" label="过期时间" show-overflow-tooltip align="center" />
				<el-table-column fixed="right" label="操作" show-overflow-tooltip width="450" align="center">
					<template #default="scope">
						<el-button size="mini" type="text" @click="openEditWindow(scope.row)">修改</el-button>
						<el-button size="mini" type="text" @click="router.push('/tenant/oauthClient?tenantId=' + scope.row.tenantId)">授权管理</el-button>
						<el-button size="mini" type="text" @click="router.push('/tenant/database?tenantId=' + scope.row.tenantId)">数据库管理</el-button>
						<el-button size="mini" type="text" @click="router.push('/tenant/miniapp?tenantId=' + scope.row.tenantId)">小程序管理</el-button>
						<el-button
							size="mini"
							type="text"
							@click="
								tenantId = scope.row.tenantId;
								overdueTime = scope.row.overdueTime;
								showDialog = true;
							"
							>续租
						</el-button>
						<el-popconfirm
							confirm-button-text="停用"
							cancel-button-text="取消"
							icon-color="red"
							title="确定停用此租户吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button size="mini" type="text">停用</el-button>
							</template>
						</el-popconfirm>
						<el-button size="mini" type="text" @click="router.push('/build?tenantId=' + scope.row.tenantId)">构建/升级</el-button>
						<el-popconfirm
							confirm-button-text="删除"
							cancel-button-text="取消"
							icon-color="red"
							title="确定删除这条记录吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button size="mini" type="text">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<!--分页-->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="params.pageNo"
				background
				v-model:page-size="params.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>

		<AddTenant ref="addRef" @initTableData="initTableData" />
		<EditTenant ref="editRef" @initTableData="initTableData" />

		<el-dialog v-model="showDialog" title="租户续租" width="30%" :before-close="dialogClose">
			<el-date-picker
				v-model="overdueTime"
				type="date"
				format="YYYY-MM-DD"
				value-format="YYYY-MM-DD 00:00:00"
				:disabledDate="disableDate"
				placeholder="请选择过期时间"
				style="width: 100%"
			/>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogClose">取消</el-button>
					<el-button type="primary" @click="dialogConfirm">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import { useRouter } from 'vue-router';
import { delTenantInfo, getTenantList, renewal } from '/@/api/tenant/info';
import AddTenant from '/@/views/tenant/info/component/addTenant.vue';
import EditTenant from '/@/views/tenant/info/component/editTenant.vue';
import { ElMessage } from 'element-plus';

export default {
	name: 'tenantInfo',
	components: {
		AddTenant,
		EditTenant,
	},
	setup(props) {
		const addRef = ref();
		const editRef = ref();
		const router = useRouter();

		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
			},
			params: {
				pageNo: 1,
				pageSize: 10,
				keyword: '',
				tenantCode: '',
				companyName: '',
			},
			tenantId: '',
			showDialog: false,
			overdueTime: '',
		});
		// 初始化表格数据
		const initTableData = () => {
			getTenantList(state.params).then((res) => {
				state.tableData.data = res.data;
				state.tableData.total = res.extra.total;
			});
		};

		//分页大小改变
		const onHandleSizeChange = (val: number) => {
			state.params.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.params.pageNo = val;
			initTableData();
		};

		// 打开新增菜单弹窗
		const openAddWindow = () => {
			addRef.value.openWindows();
		};

		const openEditWindow = (row: any) => {
			editRef.value.openWindows(row.tenantId);
		};

		const delRow = (row: any) => {
			delTenantInfo(row.tenantId).then(() => {
				initTableData();
			});
		};

		const disableDate = (time: any) => {
			// 禁用以后的时间
			return time.getTime() < Date.parse(state.overdueTime) - 8.64e6;
		};

		const dialogClose = () => {
			state.overdueTime = '';
			state.showDialog = false;
		};

		const dialogConfirm = () => {
			if (state.overdueTime == '') {
				ElMessage.error('请输入过期时间');
				return;
			}
			renewal(state.tenantId, state.overdueTime).then(() => {
				state.showDialog = false;
				initTableData();
			});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});

		return {
			addRef,
			editRef,
			router,
			initTableData,
			onHandleSizeChange,
			onHandleCurrentChange,
			openAddWindow,
			openEditWindow,
			delRow,
			disableDate,
			dialogClose,
			dialogConfirm,
			...toRefs(state),
		};
	},
};
</script>
